﻿@using System.ComponentModel
@using AntDesign.TableModels
@using ApproveFlowEngine.Approve
@using ApproveFlowEngine.Approve.DTO
@using ApproveFlowEngine.Enums.Core
@using Volo.Abp.Application.Dtos
@inject IApproveAppService _iApproveAppService

<Button Type="@ButtonType.Primary" @onclick="open">Create</Button>
<Drawer Closable="true" Width="720" Visible="visible" Title='("Create")' OnClose="_=>close()">
    <Template style="height:90%">
        <Row Gutter="16">
            <AntDesign.Col>
                <Text>Name</Text>
                <Input Placeholder="Please enter flow name" @bind-Value="@_insertWorkFlow.FlowName" TValue="string"></Input>
            </AntDesign.Col>
        </Row>
        <br />
        <Row Gutter="16">
            <AntDesign.Col>
                <Text>FlowNo</Text>
                <Input Placeholder="Please enter flow no" @bind-Value="@_insertWorkFlow.FlowNo" TValue="long" />
            </AntDesign.Col>
        </Row>
        <br />
        <Row>
            <AntDesign.Col>
                <Text>ApproveType</Text>
                <Select DataSource="@_approveTypes"
                        @bind-Value="@_insertWorkFlow.ApproveType"
                        DefaultValue="@ApproveType.General"
                        ValueProperty="c=>c.Key"
                        LabelProperty="c=>c.Value"
                        Style="width:120px">
                </Select>
            </AntDesign.Col>
        </Row>
        <br />
        <Row>
            <AntDesign.Col>
                <Text>Remark</Text>
                <InputTextArea Placeholder="Please enter flow no" @bind-Value="@_insertWorkFlow.Remark" />
            </AntDesign.Col>
        </Row>
        
        <br />
        <ApproveTree Nodes=@_insertWorkFlow.NodeTree IsEdit="true"/>   
        <br />
        <Row>
            <AntDesign.Col Span="18">

            </AntDesign.Col>
            <AntDesign.Col Span="6">
                <Button Type="default" @onclick="@Cancel">Cancel</Button>
                <Button Type="primary" @onclick="@Submit">Submit</Button>
            </AntDesign.Col>
        </Row>
    </Template>
</Drawer>

@code {
    [Parameter]
    public EventCallback OnCallBack{ get; set; }
    bool visible = false;
    NodeType nodeType = NodeType.Simple;
    private WorkFlowDto _insertWorkFlow = new WorkFlowDto();

    private Dictionary<NodeType, string> _nodeTypes = new Dictionary<NodeType, string>();
    private Dictionary<ApproveType, string> _approveTypes = new Dictionary<ApproveType, string>();

    protected override void OnInitialized()
    {
        var types = Enum.GetValues<NodeType>();
        foreach (var item in types)
        {
            _nodeTypes.Add(item, item.ToString());
        }
        var approveTypes = Enum.GetValues<ApproveType>();
        foreach (var item in approveTypes)
        {
            _approveTypes.Add(item, item.ToString());
        }
        _insertWorkFlow.NodeTree.Add(new WorkNodeDto()
            {
                Children = new List<WorkNodeDto>(),
                Index =  1,
                NodeType = NodeType.Serial,
                ApproverId = null
            });
    }

    void Cancel()
    {
        _insertWorkFlow = new WorkFlowDto();
        visible = false;
    }
    async Task Submit()
    {
        await _iApproveAppService.Create(_insertWorkFlow);
        visible = false;
        if (OnCallBack.HasDelegate)
        {
            await OnCallBack.InvokeAsync();
        }
    }
    void DeleteNode(List<WorkNodeDto>? nodes, WorkNodeDto node)
    {
        if (nodes is null)
        {
            if (_insertWorkFlow.Nodes.Count > 0)
            {
                _insertWorkFlow.Nodes.Remove(node);
            }
        }
        else
        {
            nodes.Remove(node);
        }
    }
    
    void open()
    {
        this.visible = true;
    }

    void close()
    {
        this.visible = false;
    }
    void OnSelect(TreeEventArgs<WorkNodeDto> args)
    {
        Console.WriteLine($"OnSelect {args.Node.Key}");
    }

    void OnCheck(TreeEventArgs<WorkNodeDto> args)
    {
        Console.WriteLine($"OnCheck {args.Node.Key}");
    }

    void OnContextMenu(TreeEventArgs<WorkNodeDto> args)
    {
        Console.WriteLine($"OnContextMenu {args.Node.Key}");
    }

    void SelectedNodeChanged(TreeNode<WorkNodeDto> e)
    {
        Console.WriteLine($"SelectedNodeChanged: {e?.Key}");
    }
}
